/*
*
* 三维场景的引擎 engine.js
*
* */
define(function () {
    
    var scope;
    
    var engine = function (id, object) {
        
        scope = this;
        
        var dom = document.getElementById(id);
        if (dom == undefined) return console.error("未能找到指定容器，请检测文档内是否有该id的dom!");
        
        this.dom = dom;
        
        this.init();
        
    };
    
    //可枚举属性修改器
    Object.defineProperties(engine.prototype, {});
    
    //属性增加
    Object.assign(engine.prototype, {
        
        //常量属性
        scene: new THREE.Scene(),
        cameraId: {},                        //相机管理器
        updateFun: [],                       //渲染更新包
        cameraNum: 0,                        //相机识别码
        cameraCount: 0,
        
        
        //新增场景
        addScene: function () {
            
            var view = new THREE.Scene();
            return view;
        },
        
        //新增透视相机
        addCamera: function (position) {                    //position: Array
            
            var aspect = window.innerWidth / window.innerHeight;
            var camera = new THREE.PerspectiveCamera(75, aspect, 0.5, 20000);
            if (position !== undefined) camera.position.set(position[0], position[1], position[2]);
            this.cameraCode(camera);
            this.scene.add(camera);
            return camera;
        },
        
        //新增正交相机
        addOrtCamera: function (width, height, position) {        //width:Number,height:Number,position:Array
            var aspect = window.innerWidth / window.innerHeight;
            var camera = new THREE.OrthographicCamera(width * aspect / -2, width * aspect / 2, height / 2, height / -2, 1, 200);
            if (position !== undefined) camera.position.set(position[0], position[1], position[2]);
            this.cameraCode(camera);
            this.scene.add(camera);
            return camera;
        },
        
        //相机编码器
        cameraCode: function (camera, num) {
            if (camera == undefined) return;
            if (typeof(num) === "number") {
                this.cameraId["camera" + num] = camera;
                camera.code = num;
            }
            else {
                this.cameraId["camera" + this.cameraCount] = camera;
                camera.code = this.cameraCount;
                this.cameraCount++;
                
            }
            
        },
        
        //新增环境光
        addLight: function () {
            var light = new THREE.AmbientLight(0xffffff);
            this.scene.add(light);
            return light;
        },
        
        //新增聚光
        addSpotLight: function () {
            var spotLight = new THREE.SpotLight(0xffffff);
            spotLight.castShadow = true;
            this.scene.add(spotLight);
            return spotLight;
        },
        
        //新增webgl渲染器
        addRender: function () {
            var renderer;
            renderer = new THREE.WebGLRenderer({alpha: true, antialias: true});
            // renderer.setClearColor(0x9a9a9a, 1);
            renderer.autoClear = false;
            renderer.autoUpdateScene = false;
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.setPixelRatio(window.devicePixelRatio);
            this.dom.appendChild(renderer.domElement);
            this.dom.renDom=renderer.domElement;
            this.dom.renDom.id="webgl";
            return renderer;
        },
        
        //新增canvas渲染器
        addCanvasRender: function () {
            var renderer;
            renderer = new THREE.CanvasRenderer();
            renderer.autoClear = false;
            renderer.autoUpdateScene = false;
            renderer.setSize(window.innerWidth, window.innerHeight);
            this.dom.appendChild(renderer.domElement);
            this.dom.canDom = renderer.domElement;
            renderer.domElement.style.display = "none";
            return renderer;
        },
        
        //更新相机视角
        upDateCamera: function (cameraArray) {
            var camera;
            var len = cameraArray.length;
            var i = 0;
            for (; i < len; i++) {
                camera = cameraArray[i];
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
            }
        },
        
        //相机离合器
        cameraCoupling: function (num) {
            var camera = this.cameraId["camera" + num + ""];
            if (camera == undefined) return this.cameraId["camera1"];
            return camera;
            
        },
        
        //更新队列
        update: function () {
            var i = 0;
            var len = scope.updateFun.length;
            for (; i < len; i++) {
                scope.updateFun[i]();
            }
        },
        
        //实时渲染
        render: function () {
            requestAnimationFrame(scope.render);
            scope.update();
        },
        
        //初始化
        init: function () {
            
            //初始化场景
            var scope = this;
            var camera = this.addCamera([-2000, 800, 0]);
            this.renderer = this.addRender();
            this.canvasRenderer = this.addCanvasRender();
            this.sceneHelper = this.addScene();
            this.scene.background = new THREE.Color(0x9a9a9a);
            this.scene.render = this.update;
            
            
            //初始化动态事件
            window.addEventListener('resize', onWindowResize, false);
            this.addLight();
            this.updateFun.push(
                function () {
                scope.sceneHelper.updateMatrixWorld();
                scope.scene.updateMatrixWorld();
                scope.renderer.render(scope.scene, scope.cameraCoupling(scope.cameraNum));
                if (THREE.RaytracingRenderer && scope.renderer instanceof THREE.RaytracingRenderer === false) {
                    scope.renderer.render(scope.sceneHelper, scope.cameraCoupling(scope.cameraNum));             //矢量图形渲染器
                }
            },
                function () {
                if (scope.canvas) {
                    scope.canvasRenderer.render(scope.scene, scope.cameraCoupling(scope.cameraNum));
                    scope.canvas = false;
                }
            }
            );
            
            
            this.update();
            
            //内部函数声明
            //场景自适应
            function onWindowResize() {
                scope.upDateCamera([camera]);
                scope.renderer.setSize(window.innerWidth, window.innerHeight);
                scope.canvasRenderer.setSize(window.innerWidth, window.innerHeight);
                scope.update();
            }
            
            
        }
        
    });
    
    
    return engine;
    
});